package ae.pcfc.etks.online.web.util;

import java.net.URLDecoder;
import java.net.URLEncoder;

import org.jboss.crypto.CryptoUtil;
import org.springframework.security.util.EncryptionUtils;

public class SecurityUtils {
	// Encryption Key
	public static final String SECURITY_ENCRYPTION_KEY = "1234567890!@#$%^&*()_+xy";

	public static final String hashPassword(String clearPassword) {

		String hashedPassword = CryptoUtil.createPasswordHash("SHA",
				CryptoUtil.BASE64_ENCODING, null, null, clearPassword);

		// OLD is GOLD : spring-security impl
		/*
		 * String hashedPassword; ShaPasswordEncoder encoder = new
		 * ShaPasswordEncoder(); hashedPassword =
		 * encoder.encodePassword(clearPassword, null);
		 */
		return hashedPassword;
	}

	public static final String encryptQueryParam(String clearString) {
		try {
			return URLEncoder.encode(SecurityUtils.encrypt(clearString),
					"UTF-8");
		} catch (Exception e) {
			return clearString;
		}

	}

	public static final String decryptQueryParam(String clearString) {
		try {
			return SecurityUtils.decrypt(URLDecoder
					.decode(clearString, "UTF-8"));
		} catch (Exception e) {
			return clearString;
		}

	}

	public static final String encrypt(String clearString) {
		return EncryptionUtils.encrypt(SECURITY_ENCRYPTION_KEY, clearString);
	}

	public static final String decrypt(String clearString) {
		return EncryptionUtils.decrypt(SECURITY_ENCRYPTION_KEY, clearString);
	}
}
